/**
 * Created by leo on 5/28/16.
 */

var mongoose = require('mongoose');

// Book Schema
var bookSchema = mongoose.Schema({
    title:{
        type: String,
        require: true
    },
    genre:{
        type: String,
        require: true
    },
    description:{
        type: String
    },
    author:{
        type: String,
        require: true
    },
    publisher:{
        type: String
    },
    pages:{
        type: String
    },
    image_url:{
        type: String
    },
    buy_url:{
        type: String
    },
    create_date: {
        type: Date,
        default: Date.now
    }
}, {
    // remove __v in mongodb
    versionKey: false // You should be aware of the outcome after set to false
});

var Book = module.exports = mongoose.model('Book', bookSchema);

// Get Books
module.exports.getBooks = function(callback, limit) {
    Book.find(callback).limit(limit);
}

// Get Book By Id
module.exports.getBookById = function(id, callback) {
    Book.findById(id, callback);
}

// Add Book
module.exports.addBook  = function(book, callback) {
    Book.create(book, callback);
}

// Update Book
module.exports.updateBook = function(id, book, options, callback) {
    var query = {_id: id};
    //var update = {
    //    title: book.title,
    //    genre: book.genre,
    //    description: book.description,
    //    author: book.author,
    //    publisher: book.publisher,
    //    pages: book.pages,
    //    image_url: book.image_url,
    //    buy_url: book.buy_url
    //}
    //Book.findOneAndUpdate(query, update, options, callback);
    Book.findOneAndUpdate(query, book, options, callback);
}

// Delete Book
module.exports.removeBook = function(id, callback) {
    var query = {_id: id};
    Book.remove(query, callback);
}
